﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.DataModel.Dao;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.DAC.Service;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Base.Dao;

namespace T.FAS.Runtime.DataModel.Dao
{
    public class DataObjectIndexELBDao : DaoService<DataObjectIndex>
    { 
        public override string TableName => "FAS_DataObjectIndex";
        public override string IDColumnName => "ID";
        public string ForeignColumn => "DataObjectID";

        public override List<string> ExceptColumnName {get;} = DaoUtil.GetExceptionColumn<DataObjectIndex>();

        public DataObjectIndexELBDao() : base()
        {
            DBName = "TFAS";
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<DataObjectIndex> GetDataObjectIndexs(string doId)
        {
            var result = new List<DataObjectIndex>();
            if (string.IsNullOrEmpty(doId))
            {
                return result;
            }
            List<object> objects = base.GetObjectsByFilter($"{ForeignColumn}='{doId}'");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as DataObjectIndex));
            } 

            //result.ForEach(o => {
            //    var columns = ColumnELBDao.GetDataObjectIndexColumns(o.ID);
            //    o.Columns = columns.Where(col => col.ColumnIndexType == 0).ToList();
            //    o.IncludeColumns = columns.Where(col => col.ColumnIndexType == 1).ToList();
            //});
            return result;
        }

        /// <summary>
        /// 添加DataObjectIndex
        /// </summary>
        /// <param name="doIndexs"></param>
        public void CreateDataObjectIndex(List<DataObjectIndex> doIndexs)
        {
            if (doIndexs == null) return;
            try
            {
                base.Add(doIndexs);
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DO.CreateIndexError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除DataObjectIndex
        /// </summary>
        /// <param name="DataObjectIndexs"></param>
        public void DeleteDataObjectIndex(string doId)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, "FAS_DataObjectIndexColumn", this.ForeignColumn, doId));
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, doId));
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DO.DeleteIndexError, e.Message, e);
            }
        }

    }
}
